package com.iotechn.unimall.admin.api.association;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dobbinsoft.fw.core.exception.AdminServiceException;
import com.dobbinsoft.fw.core.exception.ServiceException;
import com.dobbinsoft.fw.support.model.Page;
import com.iotechn.unimall.data.domain.AssociationDO;
import com.iotechn.unimall.data.domain.EnterpriseDO;
import com.iotechn.unimall.data.domain.UserDO;
import com.iotechn.unimall.data.dto.AssociationDTO;
import com.iotechn.unimall.data.exception.ExceptionDefinition;
import com.iotechn.unimall.data.mapper.AssociationMapper;
import com.iotechn.unimall.data.mapper.EnterpriseMapper;
import com.iotechn.unimall.data.mapper.UserMapper;
import com.iotechn.unimall.data.vo.AssociationDetailVO;
import com.iotechn.unimall.data.vo.SelectVO;
import com.iotechn.unimall.data.vo.AssociationVO;
import com.iotechn.unimall.data.vo.UserVO;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.events.Event;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AssociationServiceImpl extends ServiceImpl<AssociationMapper, AssociationDO> implements AssociationService {

    @Resource
    private EnterpriseMapper enterpriseMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public Page<AssociationVO> page(String name, Integer code, Integer page, Integer limit, Long adminId) throws ServiceException {
        QueryWrapper<AssociationDO> wrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(name)) {
            wrapper.lambda().like(AssociationDO::getName, name);
        }
        if (ObjectUtil.isNotEmpty(code)) {
            wrapper.lambda().eq(AssociationDO::getId, code);
        }
        wrapper.lambda().orderByDesc(AssociationDO::getGmtCreate);
        return baseMapper.pageAssociation(Page.div(page, limit, AssociationDO.class), wrapper);
    }

    @Override
    public String create(AssociationDTO associationDTO, Long adminId) throws ServiceException {
        AssociationDO associationDO = new AssociationDO();
        BeanUtil.copyProperties(associationDTO, associationDO);
        associationDO.setGmtCreate(new Date());
        if (baseMapper.insert(associationDO) <= 0) {
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }
        return "ok";
    }

    @Override
    public String update(AssociationDTO associationDTO, Long adminId) throws ServiceException {
        AssociationDO associationDO = new AssociationDO();
        BeanUtil.copyProperties(associationDTO, associationDO);
        associationDO.setGmtUpdate(new Date());
        if (baseMapper.updateById(associationDO) <= 0) {
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }
        return "ok";
    }

    @Override
    public String delete(Long id, Long adminId) throws ServiceException {
        QueryWrapper<EnterpriseDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(EnterpriseDO::getAssociationId, id);
        List<EnterpriseDO> enterpriseDOS = enterpriseMapper.selectList(wrapper);
        if (ObjectUtil.isNotEmpty(enterpriseDOS)) {
            throw new AdminServiceException(ExceptionDefinition.ASSOCIATION_EXIST_ENTERPRISE);
        }
        if (baseMapper.deleteById(id) <= 0) {
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }
        return "ok";
    }

    @Override
    public String batchDelete(List ids, Long adminId) throws ServiceException {
        QueryWrapper<EnterpriseDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(EnterpriseDO::getAssociationId, ids);
        List<EnterpriseDO> enterpriseDOS = enterpriseMapper.selectList(wrapper);
        if (ObjectUtil.isNotEmpty(enterpriseDOS)) {
            throw new AdminServiceException(ExceptionDefinition.ASSOCIATION_EXIST_ENTERPRISE);
        }
        if (baseMapper.deleteBatchIds(ids) <= 0) {
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }
        return "ok";
    }

    @Override
    public String richText(Long id, Long adminId) throws ServiceException {
        QueryWrapper<AssociationDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AssociationDO::getId, id);
        AssociationDO associationDO = baseMapper.selectOne(wrapper);
        return associationDO.getIntroduction();
    }

    @Override
    public List<UserVO> detail(Long id, Long adminId) throws ServiceException {
        return baseMapper.queryUserList(id);
    }

    @Override
    public List<SelectVO> associationSelect(Long adminId) throws ServiceException {
        List<AssociationDO> associationDOS = baseMapper.selectList(null);
        List<SelectVO> collect = associationDOS.stream().map(o -> {
            SelectVO selectVO = new SelectVO();
            BeanUtil.copyProperties(o, selectVO);
            return selectVO;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public String deleteUser(Long userId, Long adminId) throws ServiceException {
        QueryWrapper<UserDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(UserDO::getId, userId);

        UserDO userDO = userMapper.selectOne(wrapper);
        userDO.setUnitType(0);
        userDO.setAssociationId(null);
        if (userMapper.updateById(userDO) <= 0) {
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }
        return "ok";
    }

    @Override
    public AssociationDO queryAssociation(Long id, Long adminId) throws ServiceException {
        QueryWrapper<AssociationDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AssociationDO::getId,id);
        return baseMapper.selectOne(wrapper);
    }
}
